#include "tcStateMain.h"
#include <GL/freeglut.h>
#include <juanmabc.h>
#include "tcGame.h"
#include "tcStateOptions.h"
#include "tcStateHelp.h"
#include "tcStateCredits.h"
#include "tcStateTrack.h"

void
tcStateMainIn(void)
{
	TCGame *game;
	unsigned int strings[3];

	game = tcGame();

	tlGenStrings(3, strings);
	mlGenMaterials(1, &game->main.background);
	tlStringLoad(strings[0], PACKAGE_DATADIR);
	tlStringLoad(strings[1], L"StateMain.cmaterial");
	tlStringConcatPath(strings[2], 2, strings);
	mlMaterialLoad(game->main.background, strings[2]);
	mlMaterialGenTextures(game->main.background, strings[0]);
	tlStringsUnload(3, strings);
	tlDeleteStrings(3, strings);

	alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
}

void
tcStateMainOut(void)
{
	TCGame *game;

	game = tcGame();

	mlMaterialDeleteTextures(game->main.background);
	mlMaterialUnload(game->main.background);
	mlDeleteMaterials(1, &game->main.background);
}

void
tcStateMainKeyboard(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateMainKeyboardUp(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	switch(key)
	{
		case 27:
			glutLeaveMainLoop();
		break;
		case 13:
			switch(game->main.selection)
			{
				case 0:
					alSourcePlay(game->general.sounds[2]);
					tcStateMainOut();
					game->state = TC_STATE_OPTIONS;
					tcStateOptionsIn();
				break;
				case 1:
					alSourcePlay(game->general.sounds[2]);
					tcStateMainOut();
					game->state = TC_STATE_HELP;
					tcStateHelpIn();
				break;
				case 2:
					alSourcePlay(game->general.sounds[2]);
					tcStateMainOut();
					game->state = TC_STATE_CREDITS;
					tcStateCreditsIn();
				break;
				case 3:
					alSourcePlay(game->general.sounds[2]);
					tcStateMainOut();
					game->state = TC_STATE_TRACK;
					tcStateTrackIn();
				break;
				default:
				break;
			}
		break;
		default:
		break;
	}
}

void
tcStateMainSpecial(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	switch(key)
	{
		case GLUT_KEY_UP:
			alSourcePlay(game->general.sounds[0]);
			game->main.selection = (game->main.selection + game->main.nstrs - 1) % game->main.nstrs;
		break;
		case GLUT_KEY_DOWN:
			alSourcePlay(game->general.sounds[0]);
			game->main.selection = (game->main.selection + 1) % game->main.nstrs;
		break;
	}
}

void
tcStateMainSpecialUp(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateMainFrame(void)
{
	TCGame *game;
	unsigned int i;
	unsigned int w, h;
	float tw, th;

	game = tcGame();

	gluLookAt(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 

	mlMaterialBind(game->main.background);

	glPushMatrix();
	glScalef(0.65f * game->general.display.aspect, 0.65f * game->general.display.aspect, 0.65f * game->general.display.aspect);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0f); glVertex3f(-200.0, -200.0f, -10.0f);
	glTexCoord2f(1.0, 0.0f); glVertex3f(200.0, -200.0f, -10.0f);
	glTexCoord2f(1.0, 1.0f); glVertex3f(200.0, 200.0f, -10.0f);
	glTexCoord2f(0.0, 1.0f); glVertex3f(-200.0, 200.0f, -10.0f);
	glEnd();
	glPopMatrix();

	tw = th = 0.0f;
	for(i = 0; i < game->main.nstrs; i++)
	{
		flFontStringMetrics(game->general.font, game->main.strs[i], &w, &h);
		tw = tw >= w ? tw : w;
		th += i < game->main.nstrs - 1 ? (float) h * 1.5f : (float) h;
	}

	glPushMatrix();
	glScalef(0.65f * game->general.display.aspect, 0.65f * game->general.display.aspect, 0.65f * game->general.display.aspect);
	glTranslatef(0.0f, th / 2.0f, 0.0f);
	for(i = 0; i < game->main.nstrs; i++)
	{
		flFontStringMetrics(game->general.font, game->main.strs[i], &w, &h);

		glDisable(GL_CULL_FACE);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glMatrixMode(GL_TEXTURE);
		glPushMatrix();
		glScalef(0.1f, 0.1f, 0.1f);
		glMatrixMode(GL_MODELVIEW);

		mlMaterialBind(game->general.fontmat);

		glPushMatrix();
		glTranslatef(- (float) w / 2.0f, - (float) h, 0.0f);
		flFontDrawStringFill(game->general.font, game->main.strs[i]);
		glPopMatrix();

		glMatrixMode(GL_TEXTURE);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glEnable(GL_CULL_FACE);

		if(i == game->main.selection)
		{
			glPushMatrix();
			glTranslatef(- (float) w / 2.0f - 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef(- (float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();

			glPushMatrix();
			glTranslatef((float) w / 2.0f + 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef((float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();
		}

		glTranslatef(0.0f, - (float) h * 1.5f, 0.0f);

	}
	glPopMatrix();
}
